﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GenericValidation.Validators
{
    /// <summary>
    /// Defines a member Required validation, for mandatory members.
    /// Usually, this validation checks members against NULL values, but it also supports validating against a default value (ex. for numerical non-nullable values).
    /// </summary>
    /// <typeparam name="TEntity">Object Type.</typeparam>
    /// <typeparam name="TMember">Members Type.</typeparam>
    public class IsRequiredValidator<T, U> : MemberValidator<T, U>
        where T:class
    {
        public IsRequiredValidator(ValidationMember<T, U> member) : this(member, default(U))
        {
            ;
        }

        public IsRequiredValidator(ValidationMember<T, U> member, U defaultValue)
            : base(member)
        {
            var field = member.MemberExpression.Compile();
            this.Evaluate = (p) => field(p) != null && field(p).Equals(defaultValue) == false;
            this.Message(Templates.IsRequired, member.MemberName);
        }

        public U DefaultValue { get; set; }
    }
}
